package com.jaks.xpense.data;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;

import com.jaks.xpense.data.entities.Income;
import com.jaks.xpense.data.entities.TableEntity;
import com.jaks.xpense.utils.Globals;

public class Incomes extends Table {

	private static final String[] Columns = new String[] { TableEntity.Id, Income.IncomeDate,
	    Income.PaymentType, Income.Amount, Income.Comments };

	public Incomes(Context context) throws SQLException {
		super(context);
	}

	@Override
	public long create(TableEntity entity) {
		ContentValues values = entity.getContentValues();
		return db.insert(Income.TableName, null, values);
	}

	@Override
	public boolean update(TableEntity entity) {
		ContentValues values = entity.getContentValues();
		return db.update(Income.TableName, values, where(entity.getId()), null) > 0;
	}

	@Override
	public boolean delete(long id) {
		return db.delete(Income.TableName, where(id), null) > 0;
	}

	@Override
	public Cursor readAll() {

		String query = " Select i._id, i.incomedate, i.paymenttype, i.amount, i.comments, p.name From incomes i, paymentTypes p "
		    + " Where i.paymenttype = p._id Order By i.incomedate DESC, i._id DESC";

		return db.rawQuery(query, null);
	}

	@Override
	public TableEntity read(long id) throws SQLException {
		Income entity = null;

		Cursor cursor = db.query(true, Income.TableName, Columns, where(id), null, null, null, null,
		    null);

		if (cursor.moveToFirst()) {
			entity = new Income();
			entity.setId(cursor.getLong(cursor.getColumnIndex(TableEntity.Id)));
			entity.setIncomeDate(cursor.getString(cursor.getColumnIndex(Income.IncomeDate)));
			entity.setPaymentType(cursor.getLong(cursor.getColumnIndex(Income.PaymentType)));
			entity.setAmount(cursor.getDouble(cursor.getColumnIndex(Income.Amount)));
			entity.setComments(cursor.getString(cursor.getColumnIndex(Income.Comments)));
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}

		return entity;
	}

	public double getTotalAmount() {

		double total = 0;
		String query = "Select Sum(amount) amount From " + Income.TableName;

		Cursor cursor = db.rawQuery(query, null);
		if ((cursor.getCount() > 0) && cursor.moveToFirst()) {
			total = cursor.getDouble(cursor.getColumnIndex(Income.Amount));
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}

		return total;
	}

	public double getTotalAmount(String from, String to) {

		double total = 0;
		String query = "Select Sum(amount) amount From " + Income.TableName + " Where incomedate >= "
		    + Globals.toDbString(from) + " And incomedate <= " + Globals.toDbString(to);

		Cursor cursor = db.rawQuery(query, null);
		if ((cursor.getCount() > 0) && cursor.moveToFirst()) {
			total = cursor.getDouble(cursor.getColumnIndex(Income.Amount));
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}

		return total;
	}
}
